$(function() {

    var ParName = null;

    $.ajax({
            url: '../json/getParName.json',
            type: 'get',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: { method: 'getParName' },
        })
        .done(function(data) {
            console.log("getParName");
            ParName = data.data;

        })
        .fail(function() {
            console.log("error");
        })

    /**
      查询所有工序
    **/
    function getProcess() {
        $.ajax({
                url: '../json/getProcess.json',
                type: 'get',
                // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                data: { method: 'getProcess' },
            })
            .done(function(data) {
                console.log("getProcess");
                var data = data.data;
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    html += `<option value="${data[i]}">${data[i]}</option>`;
                }

                $(html).appendTo('.getProcess');
            }).done(function() {
                var html = "";
                for (var i = 0; i < ParName.length; i++) {
                    if ($(".getProcess").val() == ParName[i].processName) {
                        for (var j = 0; j < ParName[i].parameterName.length; j++) {
                            html += `<option value="${ParName[i].parameterName[j]}">${ParName[i].parameterName[j]}</option>`;
                        }

                    }
                }
                $(html).appendTo('.getParName');
            })
            .fail(function() {
                console.log("error");
            })
    }
    getProcess();

    $(".getProcess").change(function(event) {
        /* Act on the event */
        console.log($(this).val())
        $(".getParName").empty();
        // console.log(ParName)
        var html = "";
        for (var i = 0; i < ParName.length; i++) {
            if ($(this).val() == ParName[i].processName) {
                for (var j = 0; j < ParName[i].parameterName.length; j++) {
                    html += `<option value="${ParName[i].parameterName[j]}">${ParName[i].parameterName[j]}</option>`;
                }

            }
        }
        $(html).appendTo('.getParName');
    });


    function getResult(processName, parameterName, count, batch, usl, lsl, productlineNum, deviceNum, stationNum) {
        $.ajax({
                url: '../json/getResult.json',
                type: 'get',
                // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                data: {
                    method: 'getResult',
                    processName: processName,
                    parameterName: parameterName,
                    count: count,
                    batch: batch,
                    usl: usl,
                    lsl: lsl,
                    productlineNum: productlineNum,
                    deviceNum: deviceNum,
                    stationNum: stationNum
                },
            })
            .done(function(data) {
                console.log("getParName");
                var data = data.data;
                $(".cp").val(data.cp);
                $(".cpk").val(data.cpk);
            })
            .fail(function() {
                console.log("error");
            })

    }
    $(".button button").click(function(event) {
        /* Act on the event */
        getResult($(".getProcess").val(), $(".getParName").val(), $(".count").val(), $(".batch").val(), $(".usl").val(), $(".lsl").val(), $(".productlineNum").val(), $(".deviceNum").val(), $(".stationNum").val());
    });


})